Date.prototype.format = function (format = "yy-mm-dd HH:ii:ss") {
  var o = {
    "y+": this.getFullYear(),
    "m+": this.getMonth() + 1,
    "d+": this.getDate(), //day
    "x+": "日一二三四五六".split("")[this.getDay()],
    "H+": this.getHours(),
    h: (this.getHours() > 12 ? "下午" : "上午") + (this.getHours() % 12),
    "i+": this.getMinutes(),
    "s+": this.getSeconds(),
    S: this.getMilliseconds(),
  };
  for (var k in o) {
    var reg = new RegExp("(" + k + ")");
    if (reg.test(format)) {
      var v = o[k] + "";
      //			console.log(RegExp.$1)
      if (RegExp.$1.length < 4 && RegExp.$1.length > 1 && v.length == 1)
        v = "0" + v;
      format = format.replace(reg, v);
    }
  }
  return format;
};
Date.prototype.getToday = function () {
  return this.format("yy-mm-dd 00:00:00").toDate();
};
Date.prototype.getDayEnd = function () {
  return new Date(this.getToday() * 1 + 86400e3 - 1e3);
};
Date.prototype.getNextDay = function (gap = 1) {
  const date = new Date(
    this.getFullYear(),
    this.getMonth(),
    this.getDate() + gap
  );
  return date;
};
Date.prototype.isSameDay = function (date) {
  const fmt = "y-m-d";
  return this.format(fmt) == date.format(fmt);
};

Date.prototype.toISOFormat = function () {
  return this.format("yy-mm-ddTHH:ii:ss.SZ");
};

Date.prototype.TZ0toLocal = function () {
  const offset = new Date().getTimezoneOffset();
  return new Date(this - offset * 60e3);
};

String.prototype.toBase64 = function () {
  return Buffer.from(this).toString("base64");
};
String.prototype.decodeBase64 = function () {
  return Buffer.from(this, "base64").toString();
};

String.prototype.toDate = function () {
  const arr = this.split(/\D+/);
  if (arr.length < 3) return null;
  for (const i in arr) arr[i] = arr[i] * 1;
  arr[1] = arr[1] * 1 - 1;
  return eval(`new Date(${arr.join(",")})`);
};

String.prototype.capitalize = function () {
  return this.charAt(0).toUpperCase() + this.substr(1);
};

String.prototype.toUnicode = function () {
  return unescape(this.replace(/&#x(\w+);/g, "%u$1"));
};
String.prototype.decodeCn = function () {
  return this.replace(/&#(x)?([^&]{1,5});?/g, function ($, $1, $2) {
    return String.fromCharCode(parseInt($2, $1 ? 16 : 10));
  });
};
String.prototype.cutStr = function (pre = 100, trail = 0) {
  if (this.length <= pre + trail) return this;
  let txt = this.substr(0, pre) + "...";
  if (trail) txt += this.substr(-trail);
  return txt;
};

if (!String.prototype.replaceAll) {
  String.prototype.replaceAll = function (word, tar) {
    const reg = new RegExp(word, "mg");
    return this.replace(reg, tar);
  };
}

Array.sortArrayBy = function (arr, key, isDesc) {
  arr.sort((a, b) => {
    return (isDesc ? -1 : 1) * (a[key] - b[key]);
  });
  return arr;
};
// Array.prototype.sortBy 遍历时需增加判断 arr.hasOwnProperty(key)
